package com.example.everying.controller.user;/*
 * 项目名称: everything
 * 文件名: LabelController
 * 作者: 张泽阳
 * 日期: 2025/5/23
 * 描述:
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.dto.user.DeleteLabelDto;
import com.example.everying.model.dto.user.EditLabelDto;
import com.example.everying.model.dto.user.GetLabelByIdDto;
import com.example.everying.model.dto.user.LabelPageDto;
import com.example.everying.model.entity.user.LabelEntity;
import com.example.everying.model.vo.user.LabelVo;
import com.example.everying.service.user.LabelService;
import com.example.everying.utils.ImageUpload;
import com.example.everying.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @Autowired
    private ImageUpload imageUpload;

    @WebLog(description = "标签分页")
    @PostMapping("/page")
    public Result list(@RequestBody LabelPageDto labelPageDto) {
        System.out.println( "ssssssssssss=================="+UserContext.getUser().getId());
        Page<LabelEntity> pageParam = new Page<>(labelPageDto.getPage(), labelPageDto.getLimit());
        IPage<LabelEntity> pageModel = labelService.selectPage(pageParam, labelPageDto);
        return Result.ok(pageModel);
    }

    @WebLog(description = "获取标签图片")
    @PostMapping("/labelList")
    public Result labelList() {

        return labelService.getLabelList();
    }



    @WebLog(description = "根据id查询标签")
    @PostMapping("/getLabelById")
    public Result getLabelById(@RequestBody GetLabelByIdDto getLabelByIdDto) {
        LabelEntity labelEntity = labelService.getById(getLabelByIdDto.getId());
        if (labelEntity == null) {
            return Result.fail();
        }
        return Result.ok(labelEntity);
    }

    @WebLog(description = "添加标签")
    @PostMapping("/addLabel")
    public Result addLabel(@RequestBody EditLabelDto editLabelDto) {

        return labelService.addLabel(editLabelDto);
    }

    @WebLog(description = "修改标签")
    @PostMapping("/updateLabel")
    public Result<?> updateLabel(@RequestBody EditLabelDto editLabelDto) {
        return labelService.updateLabel(editLabelDto);
    }

    @WebLog(description = "删除标签")
    @PostMapping("/deleteLabel")
    public Result<?> deleteLabel(@RequestBody DeleteLabelDto labelVo) {
        return labelService.deleteLabelByIds(labelVo.getIds());
    }

    @WebLog(description = "上传标签图片")
    @PostMapping("/upload")
    public Result<?> uploadImages(@RequestParam("files") MultipartFile[] files) throws IOException {
        List<String> list = imageUpload.uploadImages(files);
        if (list.isEmpty()) {
            return Result.fail("上传失败");
        }
        return Result.ok(list);
    }
}
